<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="用户名">
        <el-input v-model="listQuery.q_memberName" maxlength="50" size="small" class="filter-item" />
      </el-form-item>
      <el-form-item label="是否为推广员">
        <el-select v-model="listQuery.q_isSale" size="small" placeholder="请选择">
          <el-option v-for="item in statesList" :label="item.name" :key="item.value" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="财务状态">
        <el-select v-model="listQuery.q_state" size="small" placeholder="请选择">
          <el-option v-for="item in saleMemberStatus" :label="item.name" :key="item.value" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="small" type="primary" @click="check">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="unfreeze" v-permission="['admin/sale/salemember/unfreeze']">通过审核
      </el-button>
      <el-button size="small" type="primary" @click="freeze" v-permission="['admin/sale/salemember/freeze']">审核驳回
      </el-button>
    </div>
    <el-table ref="singleTable" :data="dataList" border fit highlight-current-row v-loading="listLoading"
      style="width: 100%" @current-change="handleCurrentChange">
      <el-table-column type="index" align="center" width="50">
      </el-table-column>
      <el-table-column property="memberName" width="150" label="用户姓名"></el-table-column>
      <el-table-column property="state" width="200" label="财务审核状态">
        <template slot-scope="scope">
          {{ codeBox.SALE_MEMBER_STATE[scope.row.state] }}
        </template>
      </el-table-column>
      <el-table-column property="isSale" width="200" label="是否为推广员">
        <template slot-scope="scope">
          {{ codeBox.USE_YN[scope.row.isSale] }}
        </template>
      </el-table-column>
      <el-table-column property="referrerName" width="200" label="推荐人姓名"></el-table-column>
      <el-table-column property="referrerCode" width="200" label="推荐人的推荐码"></el-table-column>
      <el-table-column property="referrerPname" width="200" label="推荐人的推荐人的姓名"></el-table-column>
      <el-table-column property="saleCode" width="200" label="我的推广码"></el-table-column>
      <el-table-column property="auditName" width="200" label="审核人姓名"></el-table-column>
      <el-table-column property="bankType" width="200" label="开户行" show-overflow-tooltip></el-table-column>
      <el-table-column property="bankCode" width="200" label="银行卡号" show-overflow-tooltip></el-table-column>
      <el-table-column property="bankName" width="200" label="开户人姓名"></el-table-column>
      <el-table-column property="certificateCode" width="150" label="身份证号码" show-overflow-tooltip></el-table-column>
      <el-table-column property="bankAdd" width="200" label="开户行地址" show-overflow-tooltip></el-table-column>
      <el-table-column property="createTime" width="200" label="创建时间"></el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
  </div>
</template>
<script>
  import {
    fetchSaleMemberList,
    unfreezeSaleMember,
    freezeSaleMember
  } from '@/api/sale'
  import {
    getCodeManager,
    codeToArray
  } from '@/api/all'
  import Pagination from '@/components/Pagination'
  import {
    Message
  } from 'element-ui'
  export default {
    name: 'salememberlist',
    components: {
      Pagination
    },
    data() {
      return {
        dataList: [],
        saleMemberStatus: [],
        statesList: [],
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          q_memberName: '',
          q_isSale: '',
          q_state: ''
        },
        currentRow: null
      }
    },
    created() {
      getCodeManager('SALE_MEMBER_STATE', () => {
        this.saleMemberStatus = codeToArray('SALE_MEMBER_STATE');
      })
      getCodeManager('USE_YN', () => {
        this.statesList = codeToArray('USE_YN');
      })
      this.getList()
    },
    methods: {
      handleCurrentChange(val) {
        // 选中当前行数据
        this.currentRow = val
      },
      // 获取列表数据
      getList() {
        this.listLoading = true
        fetchSaleMemberList(this.listQuery).then(response => {
          this.dataList = response.data.rows
          this.total = response.data.total
          this.listLoading = false
          console.log(this.dataList)
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      check() {
        this.getList()
      },
      reset() {
        this.listQuery = {
          page: 1,
          limit: 20,
          q_memberName: '',
          q_isSale: '',
          q_state: ''
        }
        this.getList()
      },
      freeze() {
        if (!this.currentRow) {
          Message.error('请选择操作行。')
          return;
        };
        if (this.currentRow.state != 2) {
          Message.error('只有提交审核状态，才能进行操作。')
          return;
        }
        this.$confirm('确定驳回审核吗？', '提示', {
          onfirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          freezeSaleMember({
            id: this.currentRow.id
          }).then(response => {
            if (!response.data.success) {
              Message.error(res.data.message);
              return;
            }
            Message.success('操作成功')
            this.getList()
          })
        })
      },
      unfreeze() {
        if (!this.currentRow) {
          Message.error('请选择操作行。')
          return;
        };
        if (this.currentRow.state != 2) {
          Message.error('只有提交审核状态，才能进行操作。')
          return;
        }
        this.$confirm('确定通过审核吗？', '提示', {
          onfirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          unfreezeSaleMember({
            id: this.currentRow.id
          }).then(response => {
            if (!response.data.success) {
              Message.error(res.data.message);
              return;
            }
            Message.success('操作成功')
            this.getList()
          })
        })
      }
    }
  }
</script>
<style scoped>
  .filter-container .el-col-6 {
    margin-bottom: 15px;
  }

  .filter-item {
    margin-bottom: 0
  }

  /deep/ .filter-container label {
    font-size: 14px;
    font-weight: 500;
    margin-right: 10px;
  }
</style>
